package com.yuke.cloud.service.cmc.service.impl;

import com.google.common.base.Preconditions;
import com.yuke.cloud.service.api.opc.dto.UploadFile;
import com.yuke.cloud.service.api.opc.service.OpcFileFeignApi;
import com.yuke.cloud.service.cmc.dao.CmsAppNaviMapper;
import com.yuke.cloud.service.cmc.entity.CmsAppNavi;
import com.yuke.cloud.service.cmc.service.CmsAppNaviService;
import com.yuke.cloud.common.core.support.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by wg on 2019/06/21.
 */
@Service
@Transactional
public class CmsAppNaviServiceImpl extends BaseService<CmsAppNavi> implements CmsAppNaviService {
    @Resource
    private CmsAppNaviMapper cmsAppNaviMapper;

    @Resource
    private OpcFileFeignApi opcFileFeignApi;

    @Override
    public int update(CmsAppNavi cmsAppNavi) {
        int ret = super.update(cmsAppNavi);

        // 删除旧的图片文件
        if (ret > 0) {
            UploadFile uploadFile = new UploadFile();
            List<String> filePaths = new ArrayList<>();
            if (cmsAppNavi.getOldIconUrl() != null && !"".equals(cmsAppNavi.getOldIconUrl())) {
                filePaths.add(cmsAppNavi.getOldIconUrl());
            }
            if (cmsAppNavi.getOldSelIconUrl() != null && !"".equals(cmsAppNavi.getOldSelIconUrl())) {
                filePaths.add(cmsAppNavi.getOldSelIconUrl());
            }
            if (filePaths.size() > 0) {
                uploadFile.setFilePaths(filePaths);
                opcFileFeignApi.filesDelete(uploadFile);
            }
        }

        return ret;
    }

    @Override
    public void deleteById(Long id) {
        Boolean bDelete = true;

        // 判断该秒杀活动是否正在进行中，如果是则无法删除
        CmsAppNavi cmsAppNavi = findById(id);
        if (cmsAppNavi == null) {
            Preconditions.checkState(bDelete, "没有该记录，无法删除");
        }

        super.deleteById(id);

        // 删除图片文件
        UploadFile uploadFile = new UploadFile();
        List<String> filePaths = new ArrayList<>();
        if (cmsAppNavi.getOldIconUrl() != null && !"".equals(cmsAppNavi.getOldIconUrl())) {
            filePaths.add(cmsAppNavi.getOldIconUrl());
        }
        if (cmsAppNavi.getOldSelIconUrl() != null && !"".equals(cmsAppNavi.getOldSelIconUrl())) {
            filePaths.add(cmsAppNavi.getOldSelIconUrl());
        }
        if (filePaths.size() > 0) {
            uploadFile.setFilePaths(filePaths);
            opcFileFeignApi.filesDelete(uploadFile);
        }
    }

    @Override
    public void deleteByIds(String ids) {
        List<CmsAppNavi> cmsAppNavis = cmsAppNaviMapper.selectByIds(ids);
        super.deleteByIds(ids);
        UploadFile uploadFile = new UploadFile();
        List<String> filePaths = new ArrayList<>();
        for (CmsAppNavi cmsAppNavi : cmsAppNavis) {
            if (cmsAppNavi.getIconUrl() != null)
                filePaths.add(cmsAppNavi.getIconUrl());
            if (cmsAppNavi.getSelColor() != null)
                filePaths.add(cmsAppNavi.getSelColor());
        }
        uploadFile.setFilePaths(filePaths);
        opcFileFeignApi.filesDelete(uploadFile);
    }

    @Override
    public List<CmsAppNavi> getAppNavis(Byte naviType) {
        Example example = new Example(CmsAppNavi.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("sort");
        // 指定查询的属性列
        example.selectProperties("naviId", "naviName", "naviType", "iconUrl", "selIconUrl", "color", "selColor", "sort", "isEnable", "startTime", "endTime");

        Date now = new Date();

        criteria.andEqualTo("isEnable", true);
        criteria.andCondition(" UNIX_TIMESTAMP( start_time ) <= " + now.getTime() / 1000 + " AND " + now.getTime() / 1000
                + " <= UNIX_TIMESTAMP( end_time ) ");
        if (naviType != null) {
            criteria.andEqualTo("naviType", naviType);
        }

        List<CmsAppNavi> cmsAppNavis = cmsAppNaviMapper.selectByExample(example);

        return cmsAppNavis;
    }
}
